﻿Public MustInherit Class ProjectStateForm
    Protected form As ProjectForm

    Sub New(ByRef form As ProjectForm)
        Me.form = form
    End Sub

    Public Overridable Sub setForm()

    End Sub

    Public Overridable Sub loadData()
        form.txtProjectName.Text = form.project.nameProperty
        form.rtxtDescription.Text = form.project.descriptionProperty
        form.dtpStartDate.Value = form.project.estimatedFromDateProperty
        form.dtpEndDate.Value = form.project.estimatedToDateProperty
        form.txtCreateDate.Text = form.project.createDateProperty.ToString("dd/MM/yyyy")
        form.cboProjectResponsibility.SelectedValue = form.project.projectResponsibleProperty.idProperty
        form.txtState.Text = form.project.stateProperty.nameProperty

        For Each projectMember As ProjectMember In form.project.projectMembersProperty
            Dim dateFrom As String = projectMember.availableFromDateProperty
            Dim dateTo As String = projectMember.availableToDateProperty
            Dim resourceId As Integer = projectMember.resourceProperty.idProperty
            Dim resource As String = projectMember.resourceProperty.surnameProperty & ", " & projectMember.resourceProperty.firstnameProperty

            For Each projectMemberRole As ProjectMemberRole In projectMember.projectMemberRoleProperty
                Dim rolId As Integer = projectMemberRole.roleProperty.idProperty
                Dim rol As String = projectMemberRole.roleProperty.nameProperty
                form.dgvProjectMembers.Rows.Add(resourceId, resource, rolId, rol, dateFrom, dateTo)
            Next
        Next

        For Each projectActivity As ProjectActivity In form.project.projectActivitiesProperty
            Dim id As Integer = projectActivity.idProperty
            Dim projectId As Integer = projectActivity.projectProperty.idProperty
            Dim activityId As Integer = projectActivity.activityProperty.idProperty
            Dim activity As String = projectActivity.activityProperty.nameProperty
            Dim estimatedStartDate = projectActivity.estimatedFromDateProperty
            Dim estimatedEndDate = projectActivity.estimatedToDateProperty
            Dim realStartDate = projectActivity.realFromDateProperty
            Dim realEndDate = projectActivity.realToDateProperty

            form.dgvActivities.Rows.Add(id, projectId, activityId, activity, estimatedStartDate.ToString("dd/MM/yyyy"), estimatedEndDate.ToString("dd/MM/yyyy"), realStartDate.ToString("dd/MM/yyyy"), realEndDate.ToString("dd/MM/yyyy"))
        Next

    End Sub

    Public Sub setComboAndGrids()
        GenericAdministrator.loadComboBox(form.cboProjectResponsibility, ResourceAdministrator.search("id, (surname || ', ' || firstname) as name"), "id", "name")
        If form.project.projectResponsibleProperty.idProperty <> 0 Then
            form.cboProjectResponsibility.SelectedValue = form.project.projectResponsibleProperty.idProperty
        End If
        GenericAdministrator.loadComboBox(form.cboResources, ResourceAdministrator.search("id, (surname || ', ' || firstname) as name"), "id", "name")
        GenericAdministrator.loadComboBox(form.cboActivity, GenericAdministrator.searchDataByFilter(New Activity), "id", "name")
        GenericAdministrator.loadListBox(form.lbAvailableRoles, GenericAdministrator.searchDataByFilter(New Role), "id", "name")
    End Sub
End Class
